package com.example.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.dao.impl.PeopleDaoImpl;
import com.example.dao.impl.UserDaoImpl;
import com.example.dao2.PeopleDao;
import com.example.dao2.UserDao;
import com.example.entity.People;
import com.example.entity.User;
import com.google.gson.Gson;

public class PeopleServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)

			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html"); 
		response.setCharacterEncoding("UTF-8");
		//获得打印输出流
		PrintWriter pw=response.getWriter();
		PeopleDao  peopleDao=new PeopleDaoImpl();
	   //获得客户端请求参数
		//根据身份证号码查询
		People p = null;
		if (request.getParameter("idno")==null||request.getParameter("idno").equals("")) {
			String name=request.getParameter("name");
			System.out.println("正在查询------"+name);
			p=peopleDao.getPeopleByName(name);
			
		}else if((request.getParameter("name")==null||request.getParameter("name").equals(""))){
			
			String idno=request.getParameter("idno");
		    p=peopleDao.getPeopleById(idno);
		}
	
		
		if (p!=null) {
			String msg=p.toString();		
			pw.print(msg);	
			System.out.println("返回的信息是"+msg);
		    pw.flush();
		    pw.close();
		}else {
			pw.print("no record");	
		}
		
     }

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
            doGet(request, response);
	}
	//初始化方法
	public void init ()throws ServletException{
		
	}
	//构造方法
	public PeopleServlet(){
		super();
	}
	//销毁方法
	public void destory(){
		super.destroy();
	}
public String beanToJson(People people){
	
	Gson gson = new Gson();  
    System.out.println("----------简单对象之间的转化-------------");  
    // 简单的bean转为json  
    String s1 = gson.toJson(people);  
    // System.out.println("简单Bean转化为Json===" + s1);  
    return s1;

}
}
